package com.itheima.web;

import com.itheima.domain.Permission;
import com.itheima.domain.ResultInfo;
import com.itheima.domain.Role;
import com.itheima.service.impl.PermissionServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/permission")
public class PermissionServlet extends BaseServlet {
/*查看所有权限*/
    public void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            //1. 调用service查询所有权限
            PermissionServiceImpl permissionService = new PermissionServiceImpl();
            List<Permission> permissionList  = permissionService.findAll();

            //System.out.println(1 / 0);
            //2. 将集合封装到ResultInfo
            ResultInfo resultInfo = new ResultInfo(true, "查询成功", permissionList);

            //3. 将ResultInfo转成json字符串，回复给浏览器
            writeJson(response,resultInfo);

        } catch (Exception e) {
            //如果遇到问题，会执行catch，就手动封装一个ResultInfo，回复给浏览器
            e.printStackTrace();
            ResultInfo resultInfo = new ResultInfo(false, "服务器忙，稍后再试");
            writeJson(response, resultInfo);

        }


    }

}
